﻿Shader "Custom/TestShader26"
{
	Properties
	{
        _BurnAmount ("BurnAmount", range(0,1)) = 1
        _LineWidth("LineWidth", range(0, 0.2)) = 0.1
		_MainTex ("Texture", 2D) = "white" {}
        _BumpMap ("BumpMap", 2D) = "white" {}
        _BurnFirstColor("BurnFirstColor", color) = (1,0,0,1)
        _BurnSecondColor("BurnSecondColor", color) = (1,0,0,1)
        _BurnMap("BurnMap", 2D) = "white" {}
    }
        SubShader
    {
        Pass
        {
            Tags {"LightMode" = "ForwardBase"}
            Cull Off
            CGPROGRAM
            #pragma multi_compile_fwdbase
            #pragma vertex vert
            #pragma fragment frag

            #include "Lighting.cginc"
            #include "AutoLight.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _BumpMap;
            float4 _BumpMap_ST;
            sampler2D _BurnMap;
            float4 _BurnMap_ST;

            float _BurnAmount;
            float _LineWidth;

            fixed4 _BurnFirstColor;
            fixed4 _BurnSecondColor;

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 tangent : TANGENT;
                float4 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 uv : TEXCOORD0;
                float4 T2W1 : TEXCOORD1;
                float4 T2W2 : TEXCOORD2;
                float4 T2W3 : TEXCOORD3;
                float2 uvBurn : TEXCOORD4;
                SHADOW_COORDS(5)
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                o.uv.zw = v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;
                o.uvBurn = v.texcoord.xy * _BurnMap_ST.xy + _BurnMap_ST.zw;


                float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                float3 worldNormal = UnityObjectToWorldNormal(v.normal);
                float3 tangent = UnityObjectToWorldDir(v.tangent.xyz);

                float3 binormal = normalize(cross(worldNormal, tangent) * v.tangent.w);

                o.T2W1 = float4(tangent.x, binormal.x, worldNormal.x, worldPos.x);
                o.T2W2 = float4(tangent.y, binormal.y, worldNormal.y, worldPos.y);
                o.T2W3 = float4(tangent.z, binormal.z, worldNormal.z, worldPos.z);
                TRANSFER_SHADOW(o)
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed3 burn = tex2D(_BurnMap, i.uvBurn).rgb;
                clip(burn.r - _BurnAmount);
                float3 worldPos = float3(i.T2W1.w, i.T2W2.w, i.T2W3.w);
                float3 viewDir = UnityWorldSpaceViewDir(worldPos);
                float3 lightDir = UnityWorldSpaceLightDir(worldPos);

                float3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
                float3 normal = normalize(float3(dot(bump, i.T2W1.xyz), dot(bump, i.T2W2.xyz), dot(bump, i.T2W3.xyz)));

                fixed4 c = tex2D(_MainTex, i.uv.xy);

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                fixed3 diffuse = _LightColor0.rgb * saturate(dot(normal, lightDir));

                fixed t = 1 - smoothstep(0, _LineWidth, burn.r - _BurnAmount);
                fixed3 burnColor = lerp(_BurnFirstColor, _BurnSecondColor, t);
                burnColor = pow(burnColor, 5);

                UNITY_LIGHT_ATTENUATION(atten, i, worldPos);

                fixed3 finalColor = (ambient + diffuse) * c.rgb * atten;
                finalColor = lerp(finalColor, burnColor, t * step(0.001, _BurnAmount));

                return fixed4(finalColor, c.a);
            }

            ENDCG
        }

        Pass
        {
            Tags {"LightMode" = "ShadowCaster"}

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_shadowcaster
            #include "UnityCG.cginc"

            sampler2D _BurnMap;
            float4 _BurnMap_ST;
            float _BurnAmount;
            struct v2f
            {
                V2F_SHADOW_CASTER;
                float2 uvBurnMap : TEXCOORD1;
            };

            v2f vert(appdata_base v)
            {
                v2f o;
                TRANSFER_SHADOW_CASTER_NORMALOFFSET(o);
                o.uvBurnMap = v.texcoord.xy * _BurnMap_ST.xy + _BurnMap_ST.zw;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed3 burn = tex2D(_BurnMap, i.uvBurnMap).rgb;
                clip(burn.r - _BurnAmount);
                SHADOW_CASTER_FRAGMENT(i);
            }

            ENDCG
        }
	}
}
